package com.hsnn.medstgmini.trading.pur.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.dao.OrderDispatchDao;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPack;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.OrderDispatchManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 订单配送
 * @author 蔡春龙
 * @date 2015年7月14日
 */
@Service
public class OrderDispatchManagerImpl implements OrderDispatchManager {

	@Autowired
	private OrderDispatchDao orderDispatchDao;

	@Resource(name = "sessionFactory")
	private SessionFactory factory;

	/**
	 * @category 分页查询订单包信息
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @param page
	 * @return
	 */
	public Pagination getOrderDispatch(OrderPackage orderPackage,
			Pagination page) {
		page = orderDispatchDao.getOrderDispatch(orderPackage, page);
		return page;
	}

	/**
	 * @category 配送
	 * @date 2015年7月14日
	 * @param list
	 * @param user
	 */
	@Transactional
	public void dispatchDetail(OrderPack orderPack, SysUser user) {
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			orderDispatchDao.dispatchDetail(orderPack, user);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}
	/**
	 * 
	 *@category 缺货
	 *@author 邱磊
	 *@date 2015年12月13日 下午2:08:03
	 *@param 
	 *@return void
	 */
	@Transactional
	public void shortDrug(OrderPack orderPack, SysUser user){
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			orderDispatchDao.shortDrug(orderPack, user);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}

	/**
	 * @category 拒绝配送
	 * @date 2015年7月14日
	 * @param list
	 * @param user
	 */
	public void refuseDispatchDetail(OrderPack orderPack, SysUser user) {
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			orderDispatchDao.refuseDispatchDetail(orderPack, user);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}

}
